package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.MusicSinger;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.MusicSingerMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 歌手Service
 */
@Slf4j
@Service
public class MusicSingerService {

    @Autowired
    private MusicSingerMapper musicSingerMapper;

    @Autowired
    private SysUserService sysUserService;



    /**
     * 根据id查询歌手
     *
     * @param musicSinger
     * @return
     */
    public ResultData<MusicSinger> getMusicSingerById(MusicSinger musicSinger) {
        log.info("根据id查询歌手:"+ JSON.toJSONString(musicSinger));
        if(StringUtils.isEmpty(musicSinger.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicSinger.setDel(StringConstant.DEL_NO);
        MusicSinger data = musicSingerMapper.getMusicSingerById(musicSinger);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询歌手
     *
     * @param musicSinger
     * @return
     */
    public ResultData<List<MusicSinger>> getMusicSingerList(MusicSinger musicSinger) {
        log.info("多条件查询歌手:"+ JSON.toJSONString(musicSinger));
        musicSinger.setDel(StringConstant.DEL_NO);
        List<MusicSinger> data = musicSingerMapper.getMusicSingerList(musicSinger);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加歌手
     * @param musicSinger
     * @param token
     * @return
     */
    @Transactional
    public ResultData addMusicSinger(MusicSinger musicSinger,String token) {
        musicSinger.setId(StringUtil.getUUID());
        musicSinger.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicSinger.setCreateUserId(currentUser.getId());
        musicSinger.setDel(StringConstant.DEL_NO);
        log.info("添加歌手:"+ JSON.toJSONString(musicSinger));
        int i = musicSingerMapper.addMusicSinger(musicSinger);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("添加歌手失败");
        }
    }


    /**
     * 修改歌手
     *
     * @param musicSinger
     * @return
     */
    @Transactional
    public ResultData updateMusicSinger(MusicSinger musicSinger,String token) {
        if(StringUtils.isEmpty(musicSinger.getId())){
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicSinger.setUpdateUserId(currentUser.getId());
        musicSinger.setUpdateTime(new Date());
        log.info("修改歌手:"+ JSON.toJSONString(musicSinger));
        int i = musicSingerMapper.updateMusicSinger(musicSinger);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("修改歌手失败");
        }
    }


    /**
     * 删除歌手
     *
     * @param musicSinger
     * @return
     */
    @Transactional
    public ResultData delMusicSinger(MusicSinger musicSinger) {
        if(StringUtils.isEmpty(musicSinger.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicSinger.setDel(StringConstant.DEL_OK);
        log.info("删除歌手:"+ JSON.toJSONString(musicSinger));
        int i = musicSingerMapper.deleteMusicSinger(musicSinger);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("删除歌手失败");
        }
    }
}
